package com.gt.util.prop;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;

/**
 * @author WUREN
 * @desc 属性文件工具类
 * @create 2019-01-30 14:21
 **/


    public class PropUtil {
        // 默认配置文件路径classpath:config.properties
        private String properiesName = "config.properties";
        public PropUtil() {

        }

        /**
         * @param fileName properties文件全路径
         */
        public PropUtil(String fileName) {
            this.properiesName = fileName;
        }

        /**
         * 读取key对应的value值
         * @param key
         * @return
         */
        public String get(String key) {
            String value = "";
            InputStream is = null;
            try {
                is = PropUtil.class.getClassLoader().getResourceAsStream(properiesName);
                Properties p = new Properties();
                p.load(is);
                value = p.getProperty(key);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return value;
        }

        /**
         * 读取key对应的value值
         * @param key
         * @return
         */
        public String get(String key,String defaultValue) {
            String value= get(key.trim());
            if (StringUtils.isBlank(value)){
                value = defaultValue;
            }
            return value.trim();
        }

        /**
         * 返回Properties对象
         * @return
         */
        public Properties getProperties() {
            Properties p = new Properties();
            InputStream is = null;
            try {
                is = PropUtil.class.getClassLoader().getResourceAsStream(properiesName);
                p.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return p;
        }

        /**
         * 往properties文件中写入key-value键值对
         * @param key
         * @param value
         */
        public void set(String key, String value) {
            InputStream is = null;
            OutputStream os = null;
            Properties p = new Properties();
            try {
                is = new FileInputStream(PropUtil.class.getClassLoader().getResource(properiesName).getFile());
                p.load(is);
                os = new FileOutputStream(PropUtil.class.getClassLoader().getResource(properiesName).getFile());

                p.setProperty(key, value);
                p.store(os, key);
                os.flush();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != is){
                        is.close();
                    }
                    if (null != os){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
